package com.oristand.printer.address.util;

import com.oristand.printer.address.model.LabelDetail;
import com.oristand.printer.address.model.PrintRecord;
import com.oristand.printer.address.model.PrinterConfiguration;
import javafx.application.Platform;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chenkailiang
 * @date 2020/11/16
 */
public class CommonMethodUtil {

    /**
     * 拷贝选中内容到剪切板
     *
     * @param table tableview
     */
    public static void copySelectionToClipboard(TableView<?> table) {
        StringBuilder clipboardString = new StringBuilder();
        ObservableList<TablePosition> positionList = table.getSelectionModel().getSelectedCells();
        int prevRow = -1;
        for (TablePosition position : positionList) {
            int row = position.getRow();
            int col = position.getColumn();
            Object cell =  table.getColumns().get(col).getCellData(row);
            if (cell == null) {
                cell = "";
            }
            if (prevRow == row) {
                clipboardString.append('\t');
            } else if (prevRow != -1) {
                clipboardString.append('\n');
            }
            String text = cell.toString();
            clipboardString.append(text);
            prevRow = row;
        }
        final ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.putString(clipboardString.toString());
        Clipboard.getSystemClipboard().setContent(clipboardContent);
    }

    /**
     * 弹出异常信息
     * @param content 异常信息
     * @param e 异常对象
     */
    public  static void tipAlert(String content,Exception e){
        if(e != null){
            e.printStackTrace();
        }
        Alert alert = new Alert(Alert.AlertType.INFORMATION,"",new ButtonType("OK", ButtonBar.ButtonData.OK_DONE));
        alert.setTitle("Tip");
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }

    /**
     * 解析打印机配置数据
     *
     * @param rs rs
     * @return 打印机配置
     */
     static List<PrinterConfiguration> getPrinterConfigurationFromResultSet(ResultSet rs){
        List<PrinterConfiguration> printerList = new LinkedList<>();
        try {
            while (rs.next()){
                PrinterConfiguration printer = new PrinterConfiguration(new SimpleStringProperty(""),new SimpleStringProperty(""),new SimpleStringProperty(""),new SimpleStringProperty(""));
                printer.setConnectionType(rs.getString(1));
                printer.setPrinterName(rs.getString(2));
                printer.setIpAddress(rs.getString(3));
                printer.setPort(rs.getString(4));
                printerList.add(printer);
            }
        } catch (SQLException e) {
            CommonMethodUtil.tipAlert(Constants.DATABASE_ERROR,e);
        }
        return printerList;
    }

    /**
     * 接收Label信息对象
     *
     * @param rs rs
     * @return Label对象
     */
    public static List<LabelDetail> getLabelFromResultSet(ResultSet rs){
        List<LabelDetail> labelList = new LinkedList<>();
        try {
            int index = 1;
            while (rs.next()){
                LabelDetail label = new LabelDetail(new SimpleStringProperty(""+index),new SimpleStringProperty(""),new SimpleStringProperty(""),new SimpleStringProperty(""));
                label.setOrderId(rs.getString(1));
                label.setItemCode(rs.getString(2));
                label.setTrackingNumber(rs.getString(3));
                label.setPrintDate(rs.getString(4));
                labelList.add(label);
                index++;
            }
        } catch (SQLException e) {
            CommonMethodUtil.tipAlert(Constants.DATABASE_ERROR,e);
        }
        return labelList;

    }

    /**
     * 接收打印记录对象
     *
     * @param rs rs
     * @return 打印记录
     */
    public static List<PrintRecord> getPrintRecordFromResultSet(ResultSet rs){
        List<PrintRecord> recordList = new LinkedList<>();
        try {
            while (rs.next()){
                PrintRecord record = new PrintRecord(new SimpleStringProperty(""),
                        new SimpleStringProperty(""),null,new SimpleStringProperty(""),new SimpleIntegerProperty());
                record.setPrintDate(rs.getString(1));
                record.setFileName(rs.getString(2));
                record.setPrintResult(rs.getString(3));
                record.setPrintLabelCount(rs.getInt(4));
                recordList.add(record);
            }
        } catch (SQLException e) {
            Platform.runLater(()->CommonMethodUtil.tipAlert(Constants.DATABASE_ERROR,e));
        }
        return recordList;
    }


}
